<?php
namespace jy_robshop;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class team extends \plugin{
	/*
		核实分销商
	*/
	public function checkmember($mid=0,$isexi=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('team',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$teampage=$this->plugin_setting('teampage');
			$this->teamsetting=$this->plugin_setting('teamsetting');
			$teampage['tagent']=$teampage['tagent']?:'团长';
			if(!$member['status']){
				$apply=$this->get('team',array('mid'=>$_SESSION['uid']),array('id','status'));
				if($apply['id'] && $apply['status']==0){
					if($isexi){
						exi('申请正在审核请耐心等待','error','jy_robshop/team.apply/');
					}else{
						return false;
					}
				}
				$member['can_apply']=false;
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->teamsetting['become']==0){
					//分销商无需审核，此处自动审核
					$member['can_apply']=true;
					$this->apply_agent($member['mid']);
					if($this->teamsetting['isshenhe'] && $isexi){
						exi('申请正在审核请耐心等待','error','jy_robshop/team.apply/');
					}else{
						exi('','','jy_robshop/team.index/');
					}
				}elseif($this->teamsetting['become']==3){
						//参与抢购
						$iscan=pdo_count('jy_robshop_canlog',array('ispay'=>1,'mid'=>$mid));
						$member['desc']='参与抢购后';
						if($iscan){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
				}elseif($this->teamsetting['become']==4){
						//自购订单总金额
						$buyfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
						$buyfee=$buyfee?:0;
						$member['now']=$buyfee;
						$member['limit']=$this->teamsetting['buyallfee'];
						$member['unit']='元';
						if($buyfee>=$this->teamsetting['buyallfee']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
				}elseif($this->teamsetting['become']==5){
						//购买指定商品
						$become_goods=$this->teamsetting['become_goods'];
						$member['become_goods']=$become_goods;
						foreach($become_goods as &$bgv){
							$isbuy=pdo_count('jy_weishop_order_goods',array('mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1));
							if($isbuy>0){
								$member['can_apply']=true;
								$this->apply_agent($member['mid']);
								$ispass=true;
								break;
							}
						}
				}elseif($this->teamsetting['become']==2){
					$member['can_apply']=true;
					if($isexi){
						exi('你还不是'.$teampage['tagent'].'，请前往申请！','error','jy_robshop/team.apply/');
					}
				}elseif($this->teamsetting['become']==1){
					$member['desc']='暂时无法申请，只能由管理员后台指定';
				}
				if($this->teamsetting['isshenhe'] && $isexi && $ispass){
						exi('申请正在审核请耐心等待','error','jy_robshop/team.apply/');
				}elseif($ispass){
						$team=$this->get('team',array('mid'=>$_SESSION['uid']),array('id','status'));
						if($team['status']){
							exi('','','jy_robshop/team.index/');
						}
				}
				if($isexi){
						exi('你还不是'.$teampage['tagent'].'，请前往申请！','error','jy_robshop/team.apply/');
				}
			}elseif($member['stop'] && $isexi){
				exi('你的'.$teampage['tagent'].'身份已禁用！','error','jy_robshop/team.apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				$newmember=$this->get('member',array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
				if($this->teamsetting['isinfo']==1 && (!$newmember['realname'] || !$newmember['mobile'])){
					exi('请先完善信息','error','jy_robshop/team.info/');
				}else{
					
				}
			}
			return $member;
	}
	public function member_update($mid,$update=array()){
		pdo_update('jy_robshop_member',$update,array('mid'=>$mid));
		return true;
	}
	/*
		申请成为团队长
	*/
	public function apply_agent($mid,$area_code=''){
		$mid=$mid?:$_SESSION['uid'];
		$team=$this->get('team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($team['id'] && $team['status']==1){
			exi('您的申请已经通过！','error','jy_robshop/team.index/');
		}elseif($team['id'] && $team['status']==0){
			exi('您已经申请了团长！','error','jy_robshop/team.apply/');
		}
		if($team['id']){
			$new_apply=array(
				'status'=>0,
				'area_code'=>$area_code,
			);
			$this->update('team',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			$id=$team['id'];
		}else{
			$fromuser=pdo_getcolumn('jy_robshop_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>0,
				'area_code'=>$area_code,
				'fromuser'=>$fromuser?:$_SESSION['fromuser'],
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_robshop_team',$new_apply);
			$id=pdo_insertid();
			$this->update_agent_level($mid);
		}
		$this->teamsetting=$this->plugin_setting('teamsetting');
		if(!$this->teamsetting['isshenhe']){
			//分销商无需审核，此处自动审核
			$this->check_agent($id,1);
		}
		return $id;
	}
	/*
		审核团队长
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('team',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']!=1){
			$update['remark']=$remark;
		}
		$this->update('team',$update,array('id'=>$agent_id));
		if($update['status']==1){
			$this->update_agent_level($agent_area['fromuser']);
		}
		return true;
	}
	public function set_parent($mid,$fromuser=0){
		$mid=$mid?:$_SESSION['uid'];
		if($mid==$fromuser || !$fromuser){
			return false;
		}
		$ta_agent_id=$this->getcolumn('member',array('mid'=>$fromuser),'fromuser');
		//如果他的上级是我
		if($ta_agent_id==$mid){
			return true;
		}
		$this->update('team',array('fromuser'=>$fromuser,'updatetime'=>TIMESTAMP),array('mid'=>$mid));
		$this->update_agent_level($fromuser);
		return true;
	}
	/*
		禁用团长
	*/
	public function stop_agent($agent_id){
		$this->update('team',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复团长
	*/
	public function start_agent($agent_id){
		$this->update('team',array('stop'=>0),array('mid'=>$agent_id));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid,$self=false){
		//
		if(!$mid){
			return;
		}
		$agent=$this->get('team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if(!$agent['mid']){
			return;
		}
		if($agent['status']!=1 && !$self){
			return;
		}
		$level=$this->get('teamlevel',array('id'=>$agent['level_id']));
		$level['level']=$level['level']?:0;
		$level_up_types=$this->model('model')->team_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$levels=$this->getall('teamlevel',array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),'','','level desc');
		if(empty($levels)){
			return;
		}
		$teamids=$this->getteamids($agent['mid']);
		if(!$teamids){
			$teamids=-1;
		}
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=true;
			foreach($level_up_types as $type){
				if($type=='myorder_price'){
					//抢购抢中N元
					$agent['myorder_price']=pdo_getsum('jy_robshop_canlog',array('uniacid'=>$_SESSION['uniacid'],'status'=>2,'ispay'=>1,'mid'=>$agent['mid']),'fee');
					if($conditions['myorder_price'] && $agent['myorder_price']<$conditions['myorder_price']){
						$up=false;
						break;
					}
				}elseif($type=='agent_num'){
					//名下N个次等级团长
					$clevel=$this->get('teamlevel',array('uniacid'=>$_SESSION['uniacid'],'level <'=>$level['level']),array('id'),'level desc');
					$clevel_id=$clevel['id'];
					$agent['agent_num']=pdo_count('jy_robshop_team',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$clevel_id,'mid'=>$teamids));
					if($agent['agent_num']<$conditions['agent_num'] && $conditions['agent_num']){
						$up=false;
						break;
					}
				}elseif($type=='level_num'){
					//平一级人数
					$agent['level_num']=pdo_count('jy_robshop_team',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$agent['level_id'],'mid'=>$agent['mid']));
					if($agent['level_num']<$conditions['level_num'] && $conditions['level_num']){
						$up=false;
						break;
					}
				}elseif($type=='order_price'){
					//名下总业绩N元
					$agent['order_price']=pdo_getsum('jy_robshop_canlog',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$teamids),'fee');
					if($conditions['order_price'] && $agent['order_price']<$conditions['order_price']){
						$up=false;
						break;
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
				$update=array(
					'level_id'=>$next_level['id'],
					'status'=>1,
				);
				$this->update('team',$update,array('id'=>$agent['id']));
				$this->update_agent_level($agent['fromuser']);
		}
		return true;
	}
	public function check_cangetfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		$this->teamsetting=$this->plugin_setting('teamsetting');
		if($type==5){
			for($i=0;$i<5;$i++){
				$res=$this->check_file($this->teamsetting['daili_file'.$i],$i);
				if($res){
					return true;
				}
			}
		}else{
			for($i=0;$i<5;$i++){
				$res=$this->check_file($this->teamsetting['ping_file'.$i],$i);
				if($res){
					return true;
				}
			}
		}
		return false;
	}
	public function check_file($file,$key=0){
			$mid=$_SESSION['uid'];
			if(!$file){
				return false;
			}
			//直推预选团队长多少人
			if($key==0){
				$area_num=$this->count('team',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid));
				if($file<=$area_num){
					return true;
				}
			}elseif($key==1){
				//分红金额满多少元
				$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid);
				$ji_money=$this->sum('commission',$params,'money');
				if($file<=$ji_money){
					return true;
				}
			}elseif($key==2){
				//二级团队长满多少人
				$agent1_ids=$this->getall('team',array('fromuser'=>$mid),array('mid'),'mid');
				$agent1_ids=array_keys($agent1_ids);
				$agent2_num=$this->count('team',array('fromuser'=>$agent1_ids));
				if($file<=$agent2_num){
					return true;
				}
				
			}elseif($key==3){
				//三级团队长满多少人
				$agent1_ids=$this->getall('team',array('fromuser'=>$mid),array('mid'),'mid');
				$agent1_ids=array_keys($agent1_ids);
				$agent2_ids=$this->getall('team',array('fromuser'=>$agent1_ids),array('mid'),'mid');
				$agent2_ids=array_keys($agent2_ids);
				$agent3_num=$this->count('team',array('fromuser'=>$agent2_ids));
				if($file<=$agent3_num){
					return true;
				}
				
			}elseif($key==4){
				//四级团队长满多少人
				$agent1_ids=$this->getall('team',array('fromuser'=>$mid),array('mid'),'mid');
				$agent1_ids=array_keys($agent1_ids);
				$agent2_ids=$this->getall('team',array('fromuser'=>$agent1_ids),array('mid'),'mid');
				$agent2_ids=array_keys($agent2_ids);
				$agent3_ids=$this->getall('team',array('fromuser'=>$agent2_ids),array('mid'),'mid');
				$agent3_ids=array_keys($agent3_ids);
				$agent4_num=$this->count('team',array('fromuser'=>$agent3_ids));
				if($file<=$agent4_num){
					return true;
				}
			}elseif($key==5){
				//商城自购金额满
				$buyfee=pdo_getsum('jy_weishop_order',array('mid'=>$mid,'ispay'=>1),'price');
				if($file<=$buyfee){
					return true;
				}
			}elseif($key==6){
				//直推获得拼团金满
				if(!$this->plugin_exists('jy_robshop')){
					return true;
				}
				$agent1_ids=$this->getall('team',array('fromuser'=>$mid),array('mid'),'mid');
				if($agent1_ids){
					$agent1_ids=array_keys($agent1_ids);
				}else{
					$agent1_ids=-1;
				}
				$fee=pdo_getsum('jy_robshop_feelog',array('mid'=>$agent1_ids,'type'=>3),'fee');
				if($file<=$fee){
					return true;
				}
			}elseif($key==7){
				//直推累计购物满
				$agent1_ids=$this->getall('team',array('fromuser'=>$mid),array('mid'),'mid');
				if($agent1_ids){
					$agent1_ids=array_keys($agent1_ids);
				}else{
					$agent1_ids=-1;
				}
				$buyfee=pdo_getsum('jy_weishop_order',array('mid'=>$agent1_ids,'ispay'=>1),'price');
				if($file<=$buyfee){
					return true;
				}
			}
		return false;
	}
	//能否再获得奖金
	public function check_canaddfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		$this->teamsetting=$this->plugin_setting('teamsetting');
		if($type==5){
			if($this->teamsetting['daili_jie']){
				//封顶时，满额了
				if($this->teamsetting['daili_type']==1){
					$fee=$this->sum('commission',array('type'=>5,'mid'=>$mid,'status <='=>1),'money');
					if($fee>=$this->teamsetting['daili_manfee']){
						return false;
					}
				}
			}else{
				$isgetfee=$this->check_isgetfee($mid,$type);
				if($isgetfee){
					return false;
				}
			}
		}
		if($type==6){
			if($this->teamsetting['ping_jie']){
				//封顶时，满额了
				if($this->teamsetting['ping_type']==1){
					$fee=$this->sum('commission',array('type'=>6,'mid'=>$mid,'status <='=>1),'money');
					if($fee>=$this->teamsetting['ping_manfee'] && $this->teamsetting['ping_manfee']){
						return false;
					}
				}
			}else{
				$isgetfee=$this->check_isgetfee($mid,$type);
				if($isgetfee){
					return false;
				}
			}
		}
		return true;
	}
	public function check_isgetfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		$params=array(
			'status'=>3,
			'fee_type'=>$type,
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
		);
		$balance=$this->get('balance',$params);
		if($balance['id']){
			return true;
		}else{
			return false;
		}
	}
	public function createfee($canlog=array()){
		if($canlog['fee']<=0){
			return false;
		}
		$member=$this->get('member',array('mid'=>intval($canlog['mid'])));
		$fromuser=$member['fromuser'];
		if(!$fromuser){
			return false;
		}
		$this->canlog=$canlog;
        $this->qz_fee=0;
		$this->ji_fee=0;
		$this->isping=0;
		$this->lastjiee=0;
        $this->nowlevel=array();
        $level=pdo_get('jy_robshop_teamlevel',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['level_id']));
        if($level['ping_per']>0){
            $this->nowlevel=$level['level_id'];
        }
		$levelnum=12;
		//极差分红单线等级ABCDE，第一级没有分红，低于自己的没有分红，平级没有分红
		for($i=0;$i<$levelnum;$i++){
				if(!$fromuser){
					break;
				}
				$team=$this->get('team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
				$fromuser=$this->getcolumn('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'fromuser');
				$this->up_fee_per($team,$i);
		}
		return true;
	}
	//直推抢中,平级分红，极差分红
	public function up_fee_per($team=array(),$i=0){
		$level=pdo_get('jy_robshop_teamlevel',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['level_id']));
		$fee_per=$level['fee_per'];
		$ye_per=$level['ye_per'];
		$ping_per=$level['ping_per'];
		$canlog=$this->canlog;
		$fee=$canlog['fee'];
		if($fee<=0){
			return false;
		}
		$team_goods=$this->get('team_goods',array('uniacid'=>$_SESSION['uniacid'],'goods_id'=>$canlog['goodsid']));
		if($team_goods['id'] && !$team_goods['ignore']){
			$commissions=$team_goods['commissions']?json_decode($team_goods['commissions'],true):array();
			foreach($commissions as $gc){
				if($gc['level_id']==0){
					$demogoods_fee_per=$gc['fee_per'];
					$demogoods_ye_per=$gc['ye_per'];
					$demogoods_ping_per=$gc['ping_per'];
				}
				if($gc['level_id']!=$team['level_id']){
					continue;
				}else{
					$goods_fee_per=$gc['fee_per'];
					$goods_ye_per=$gc['ye_per'];
					$goods_ping_per=$gc['ping_per'];
					break;
				}
			}
			$goods_fee_per=$goods_fee_per?:$demogoods_fee_per;
			$goods_ye_per=$goods_ye_per?:$demogoods_ye_per;
			$goods_ping_per=$goods_ping_per?:$demogoods_ping_per;
			if(substr($goods_fee_per,-1)=='%'){
				$fee1=($fee*substr($goods_fee_per,0,-1))/100;
			}else{
				$fee1=$goods_fee_per;
			}
			if(substr($goods_ye_per,-1)=='%'){
				$fee3=($fee*substr($goods_ye_per,0,-1))/100;
			}else{
				$fee3=$goods_ye_per;
			}
			if(substr($goods_ping_per,-1)=='%'){
				$fee6=($fee*substr($goods_ping_per,0,-1))/100;
			}else{
				$fee6=$goods_ping_per;
			}
		}
		//直推抢中(极差)
		if($fee>0 && $fee_per>0){
			$check_kao=$this->check_kao($team['mid'],1);
			if($team_goods['id']){
				$money=$fee1;
			}else{
				$money=$fee*$fee_per/100;
			}
            $money -=$this->qz_fee;
			if($money>0 && $check_kao){
                $this->qz_fee +=$money;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$team['mid'],
						'goodsid'=>$canlog['goodsid'],
						'buymid'=>$canlog['mid'],
						'canlogid'=>$canlog['id'],
						'money'=>$money,
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>1,
				);
				$this->insert('commission',$new_commission);
			}
		}
		//平级分红
		if(!$this->isping && $team['level_id'] && $this->nowlevel[$team['level_id']] && $fee>0 && $ping_per>0){
			if($team_goods['id']){
				$money=$fee3;
			}else{
				$money=$fee*$ping_per/100;
			}
			$check_kao=$this->check_kao($team['mid'],3);
			if($money>0 && $check_kao){
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$team['mid'],
						'goodsid'=>$canlog['goodsid'],
						'buymid'=>$canlog['mid'],
						'canlogid'=>$canlog['id'],
						'money'=>$money,
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>3,
				);
				$this->insert('commission',$new_commission);
				$this->isping=1;
			}
		}
		//极差分红
		if($fee>0 && $ye_per>0){
			if($team_goods['id']){
				$money=$fee6;
			}else{
				$money=$fee*$ye_per/100;
			}
			$money -=$this->ji_fee;
			$check_kao=$this->check_kao($team['mid'],6);
			if($money>0 && $check_kao){
				$this->ji_fee +=$money;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$team['mid'],
						'goodsid'=>$canlog['goodsid'],
						'buymid'=>$canlog['mid'],
						'canlogid'=>$canlog['id'],
						'money'=>$money,
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>6,
				);
				$this->insert('commission',$new_commission);
				$this->lastjiee=$money;
			}
		}
        if($level['ping_per']>0){
            $this->nowlevel[$team['level_id']]=1;
        }
		return true;
	}
	//直推抢拍（极差）
	public function up_red_per($canlog=array(),$redfee=0){
		//已注销
		return false;
		if($redfee<=0){
			return false;
		}
        $this->qp_fee=0;
        $levelnum=12;
        $member = $this->get('member', array('mid' => intval($canlog['mid'])));
        $fromuser = $member['fromuser'];
        if (!$fromuser) {
            return false;
        }
        for($i=0;$i<$levelnum;$i++) {
            $team = $this->get('team', array('uniacid' => $_SESSION['uniacid'], 'mid' => $fromuser));
            $fromuser=$this->getcolumn('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'fromuser');
            $level = pdo_get('jy_robshop_teamlevel', array('uniacid' => $_SESSION['uniacid'], 'id' => $team['level_id']));
            $red_per = $level['red_per'];
            if ($redfee > 0 && $red_per > 0) {
                $money = $redfee * $red_per / 100;
                $money -=$this->qp_fee;
                if ($money > 0) {
                    $this->qp_fee +=$money;
                    $new_commission = array(
                        'uniacid' => $_SESSION['uniacid'],
                        'mid' => $team['mid'],
                        'goodsid' => $canlog['goodsid'],
                        'buymid' => $canlog['mid'],
                        'canlogid' => $canlog['id'],
                        'money' => $money,
                        'createtime' => TIMESTAMP,
                        'status' => 1,
                        'type' => 2,
                    );
                    $this->insert('commission', $new_commission);
                }
            }
        }
		return true;
	}
	//加权分红
	public function get_store_per(){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isget=pdo_get('jy_robshop_commission',array('createtime >'=>$daytime,'type'=>4,'uniacid'=>$_SESSION['uniacid']),'id');
		if($isget['id']){
			return false;
		}
		$levelids=$this->getall('teamlevel',array('isjiared'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','isjiared'),'id');
		if(!$levelids){
			return false;
		}
		$levelids=array_keys($levelids);
		$teams=$this->getall('team',array('level_id'=>$levelids,'uniacid'=>$_SESSION['uniacid']));
		$num=count($teams);
		if(!$num){
			return false;
		}
		//获取加权分红
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>2);
		$params['createtime between']=array($daytime-24*3600,$daytime);
		$money=pdo_getsum('jy_robshop_canlog',$params,'fee');
		//加权分红池总金额
		if($money<=0 || $money<=0.01){
			return false;
		}
		$teamsetting=$this->plugin_setting('teamsetting');
		$store_per=$teamsetting['store_per'];
		if($store_per<=0){
			return false;
		}
		//加权分红池总金额
		$redallfee=$money*$store_per/100;
		if($redallfee>0.01){
			$allteamfee=0;
			foreach($teams as &$item){
				$teamids=$this->getteamids($item['mid']);
				if($teamids){
					$params['mid']=$teamids;
				}else{
					$params['mid']=$item['mid'];
				}
				$item['teammoney']=pdo_getsum('jy_robshop_canlog',$params,'fee');
				$allteamfee +=$item['teammoney'];
			}
			//每1元加权分红值
			$oneyunper=$redallfee/$allteamfee;
			foreach($teams as $tv){
				$check_kao=$this->check_kao($tv['mid'],4);
				if(!$check_kao){
					continue;
				}
				//个人所得加权分红收益
				$myfee=$tv['teammoney']*$oneyunper;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$tv['mid'],
					'money'=>$myfee,
					'type'=>4,
					'remark'=>'加权分红',
					'createtime'=>TIMESTAMP,
					'status'=>1,
				);
				$this->insert('commission',$new_commission);
			}
		}
		return true;
	}
	public function getteamids($teammid){
		$newmembers=array();
		$newteammids=$teammid;
		for($i=1;$i<12;$i++){
			$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$newteammids),array('mid'),'mid');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=array();
		}
		return $teamids;
	}
	//首次参与抢购多少元，才能享受会员价格购买
	public function check_vipbuy($mid=0,$team=array()){
		$this->teamsetting=$this->plugin_setting('teamsetting');
		$mid=$mid?:$_SESSION['uid'];
		$team=$this->get('team',array('mid'=>$mid,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
		if(!$team['id']){
			return false;
		}
		$vip_fee=$this->teamsetting['vip_fee'];
		if(!$vip_fee || $vip_fee<=0){
			return true;
		}
		$startfee=pdo_get('jy_robshop_canlog',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'fee');
		if($startfee['fee']<$vip_fee){
			return false;
		}
		return true;
	}
	//月度考核
	public function check_kao($mid=0,$type=0){
		$this->teamsetting=$this->plugin_setting('teamsetting');
		$mid=$mid?:$_SESSION['uid'];
		$kao_num=$this->teamsetting['kao_num'];
		$kao_feetype=$this->teamsetting['kao_feetype'];
		if(!$kao_num || !$kao_feetype){
			return true;
		}
		$month_start=strtotime(date('Ym').'01');
		$iscannum=pdo_count('jy_robshop_canlog',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$month_start,'ispay'=>1,'mid'=>$mid),'id');
		if($iscannum<$kao_num){
			if(strpos($kao_feetype, ';'.$type) !== false){
				return false;
			}
		}
		return true;
	}
}